// import './polyfills/buffer';
import { createProjectBuilder } from './generator/ProjectBuilder';

import esmodule from './plugins/common/esmodule';
// import styleImport from './plugins/common/styleImport';
import containerClass from './plugins/component/react/containerClass';
import containerInitState from './plugins/component/react/containerInitState';
import containerInjectContext from './plugins/component/react/containerInjectContext';
// import containerInjectUtils from './plugins/component/react/containerInjectUtils';
// import containerInjectDataSourceEngine from './plugins/component/react/containerInjectDataSourceEngine';
// import containerInjectConstants from './plugins/component/react/containerInjectConstants';
// import containerInjectI18n from './plugins/component/react/containerInjectI18n';
import containerLifeCycle from './plugins/component/react/containerLifeCycle';
import containerMethod from './plugins/component/react/containerMethod';
import containerRender from './plugins/component/react/containerRender';
import containerEvalError from './plugins/component/react/containerEvalError';
import reactCommonDeps from './plugins/component/react/reactCommonDeps';
import css from './plugins/component/style/css';

const appBuilder = createProjectBuilder({
  plugins: {
    components: [ // schema 生成 content 的逐行指令
      reactCommonDeps(),              //  import react from "react";
      esmodule({ fileType: 'jsx' }),  //  import { CompName } from "lion-components";
      css(),                          //  import './index.css'
      containerClass(),               //  class RootComponent$$id extends React.Component {
                                      //    static displayName = 'LionABCComponent';
      containerInjectContext(),       //    _context = this;
      containerInitState(),           //    this.state = { data: this.props.data, text: 'outer', isShowDialog: false };
      containerLifeCycle(),           //    componentDidMount(){ ... } 
                                      //    componentWillUnmount(){ ... }
      containerMethod(),              //    onClick2(){ ... }
      containerRender({               //    render(){ return (<div><CompName /></div>) }
        nodeTypeMapping: {
          Div: 'div',
          Component: 'div',
          Page: 'div',
          Block: 'div',
        },
      }),
      containerEvalError(),           //  __$$eval(expr){ ... }
    ]
  }
});

export default async (schema: any) => {
  return new Promise(async (resolve) => {
    const finalResult = await appBuilder.generateProject(schema);
    resolve(finalResult);
  });
}


